<?php
/**
 * @category   Delcampe
 * @package    Delcampe_Translate_Adapter_DB
 *
 * @copyright Sebastien Delcampe
 * @license Please, don't read or copy
 * @author Christophe Gesché
 *
 * @version 1.0
 **/
//include ('Zend/Translate/Adapter.php');

class Lion_Translate_Adapter_Db extends Zend_Translate_Adapter
{

    /**
     * Enter description here...
     *
     * @param unknown_type $data
     * @param unknown_type $locale
     * @param array $options
     */
    public function __construct ($data, $locale = null, array $options = array())
    {

        $registry = Zend_Registry::getInstance();
        $this->db = $registry->get('db');
        parent::__construct($data, $locale, $options);
    }

    /**
     * Enter description here...
     *
     * @param unknown_type $data
     * @param unknown_type $locale
     * @param array $options
     */
    public function _loadTranslationData ($data, $locale, array $options = array())
    {

       /* $strData = $this->db->fetchAll('
            SELECT lk.keyText,
                   l.contents
            FROM       td_labels     AS l
            INNER JOIN td_labels_key AS lk
            ON    l.language = ?', $locale);*/
       
		$sess = new Zend_Session_Namespace('locale');
		$curr_lang = $sess->current_lang;
			
		if(isset($curr_lang))
	    {
	    	$lang = $curr_lang;
	    } else {
	    	$lang = 'en';
	    }
	    
	    $strData = $this->db->fetchAll('
       		SELECT l.contents, lk.keyText
          	FROM lionparadise.td_labels_key AS lk
          	LEFT JOIN lionparadise.td_labels AS l          
          	ON l.id = lk.id          
			WHERE l.language = ?', $lang);        
        
      $strScheme = array();
      if (! empty($strData)) {
	      foreach ($strData as $trEl) {
	      	//$strScheme[$trEl[$data['joinField']]] = $trEl[$data['valueField']];
	      	$strScheme[$trEl['keyText']] = $trEl['contents'];
	      }
      }
      $data = $strScheme;
      $options = array_merge($this->_options, $options);
      if (($options['clear'] == true) || ! isset($this->_translate[$locale])) {
        $this->_translate[$locale] = array();
      }
      
      $this->_translate[$locale] = array_merge($this->_translate, $data);
      //return $this->_translate; 
      echo $this->_translate;
    }

    /**
     * Enter description here...
     *
     * @return string
     */
    public function toString ()
    {

        return 'Db';
    }
}
